<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <style type="text/css">
   * {
       font-family: 'Open Sans', sans-serif;
   }
  .center-grey {
      background: #f2f2f2;
   }
   .slider {
        position: relative;
        padding: 5px;
        width: 800px;
        margin: auto;
        margin-top: 40px;
        }
    .slide {
        position: absolute;
        top: 0;
        left: 0;
        box-shadow: 0px 0px 15px #999;
    }
    .arrows {
        font-size: 26px;
        }
    .arrows > a {
        position: absolute;
        top: 200px;
        color: #fff;
    }
    a.disabled {
        color: #666;
    }
    .arrows > a:first-child {
        left: 20px;
        }
    .arrows > a:last-child {
        right: 20px;
        }
    h1 {
        text-align: center;
        padding: 10px;
        font-size: 40px;
        color: #222;
    }
    .slide.ng-hide-add,
    .slide.ng-hide-remove {
        -webkit-transition: all linear 0.5s;
        -moz-transition: all linear 0.5s;
        -o-transition: all linear 0.5s;
        transition: all linear 0.5s;
        display: block!important;
        }
    .slide.ng-hide-add.ng-hide-add-active,
    .slide.ng-hide-remove {
        opacity: 0;
        }
    .slide.ng-hide-add,
    .slide.ng-hide-remove.ng-hide-remove-active {
        opacity: 1;
        }
     .slider-thumb {
        position: absolute;
        left: 0;
        right: 0;
        top: 420px;
        text-align: center;
        font-size: 16px;
        }
    .slider-thumb > a {
        color: #fff;
    }
    .slider-thumb > a.curr {
        color: #f00;
    }
    </style>
    <style>
        .demo{
            margin-bottom:10px;
            border-bottom:1px solid #1b926c;
        }
    </style>
</head>
<body>

 <div class="container" ng-app="myApp">
     <div class="row">
         <nav class="navbar navbar-default navbar-static-top">
             <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="../index.html">首页</a>
                 </div>
                 <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                 </ul>
             </div>
         </nav>
     </div>
     <div class="row demo">
         <h3>Demo1: slider效果</h3>
         <div ng-controller="sliderCtrl">
             <slider images="images" auto="true"></slider>
         </div>
     </div>
 </div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script src="http://cdn.bootcss.com/angular.js/1.4.0-beta.4/angular-1.4.0-beta.5/angular-animate.min.js"></script>
<script>
    var myApp = angular.module("myApp",["ngAnimate"]);
    myApp.controller("sliderCtrl",["$scope","$http",function($scope,$http){
        $scope.images = [];
        //造些假数据,真是的你可以是$http 远程拉取数据
        for (var i = 1; i <= 8; i++) {
            $scope.images.push({
                "src":"images/"+ i + ".jpg"
            });
        }
    }]);
    //
    myApp.directive("slider",function($timeout){
        return  {
            restrict:"EA",
            replace:true,
            scope:{
                images:"="
            },
            templateUrl:"slider.tpl.html",
            link:function(scope,element,attrs){
                scope.current = 0;
                scope.totalNumber = scope.images.length;
                scope.next = function(){
                    if(scope.current < scope.totalNumber - 1){
                        scope.current = scope.current + 1;
                    }else{
                        scope.current = 0;
                    }
                };
                scope.prev = function(){
                    if(scope.current > 0){
                        scope.current = scope.current - 1;
                    }else{
                        scope.current = scope.totalNumber - 1;
                    }
                };
                //
                var timer = null;
                var autoPlay = function(){
                    timer = $timeout(function(){
                        scope.next();
                        //
                        timer = $timeout(autoPlay,1000 * 3);
                    },1000 * 3)
                };
                //autoPlay();
                if(attrs.auto == "true"){
                    autoPlay();
                };
                //鼠标移上去停止播放
                element.on("mouseover",function(){
                    $timeout.cancel(timer);
                });
                //鼠标移出继续播放
                element.on("mouseout",function(){
                    autoPlay();
                });
                //
                scope.$on('$destroy',function(){
                    $timeout.cancel(timer);
                });
            }
        }
    });
</script>

